public class TestSort {
    public static void SelectSort(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            int min=i;
            for (int j = i+1; j < arr.length; j++) {
                if(arr[j]<arr[min]){
                    min=j;
                }
            }
            swap(arr,i,min);
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp=arr[i];
        arr[i]=arr[j];
        arr[j]=tmp;
    }
    public static void SelectSort2(int[] arr){
        int left=0;
        int right=arr.length-1;
        while(left<right){
            int min=left;
            int max=left;
            for (int i = left+1; i <= right; i++) {
                if(arr[i]<arr[min]){
                    min=i;
                }
                if(arr[i]>max){
                    max=i;
                }
            }
            swap(arr,left,min);
            if(max==left){
                max=min;
            }
            swap(arr,right,max);
            left++;
            right--;
        }
    }
    public static void heapSort(int[] arr){
        createHeap(arr);
        int end=arr.length-1;
        while(end>0){
            swap(arr,0,end);
            siftDown(arr,0,end);
            end--;
        }
    }
    public static void createHeap(int[] arr){
        for (int parent = (arr.length-1-1)/2; parent >= 0 ; parent--) {
            siftDown(arr,parent,arr.length);
        }
    }

    private static void siftDown(int[] arr, int parent, int length) {
        int child=parent*2+1;
        while(child<length){
            if(child+1<length&&arr[child]<arr[child+1]){
                child++;
            }
            if(arr[child]>arr[parent]){
                swap(arr,child,parent);
                parent=child;
                child=parent*2+1;
            }else {
                break;
            }
        }
    }
    public static void BubbleSort(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            boolean flg=false;
            for (int j = 0; j < arr.length-1-i ; j++) {
                if(arr[j]>arr[j+1]){
                    swap(arr,j,j+1);
                    flg=true;
                }
            }
            if(!flg){
                break;
            }
        }
    }
    public static void quickSort(int[] arr){
        quick(arr,0,arr.length-1);
    }

    private static void quick(int[] arr, int start, int end) {
        if(start>=end){
            return;
        }
        int pivot=partition(arr,start,end);
        quick(arr,start,pivot-1);
        quick(arr,pivot+1,end);
    }

    private static int partition(int[] arr, int left ,int right) {
        int tmp=arr[left];
        int tmpIndex=left;
        while(left<right){
            while(left<right&&arr[right]>=tmp){
                right--;
            }
            while (left<right&&arr[left]<=tmp){
                left++;
            }
            swap(arr,left,right);
        }
        swap(arr,tmpIndex,left);
        return left;
    }
    public static void mergeSort(int[] arr){
        mergeTmp(arr,0,arr.length-1);
    }

    private static void mergeTmp(int[] arr, int left, int right) {
        if(left>=right){
            return;
        }
        int mid=(left+right)/2;
        mergeTmp(arr,left,mid);
        mergeTmp(arr,mid+1,right);
        merge(arr,left,mid,right);
    }

    private static void merge(int[] arr, int left, int mid, int right) {
        int[] tmp=new int[right-left+1];
        int k=0;
        int s1=left;
        //int e1=mid;
        int s2=mid+1;
        //int e2=right;
        while (s1<=mid&&s2<=right){
            if(arr[s1]<arr[s2]){
                tmp[k++]=arr[s1++];
            }else {
                tmp[k++]=arr[s2++];
            }
        }
        while (s1<=mid){
            tmp[k++]=arr[s1++];
        }
        while (s2<=right){
            tmp[k++]=arr[s2++];
        }
        for (int i = 0; i < k; i++) {
            arr[i+left]=tmp[i];
        }
    }
    public static void mergeSortNor(int[] arr){
        int gap=1;
        while(gap<arr.length){
            for (int i = 0; i < arr.length; i=i+gap*2) {
                int left=i;
                int mid=left+gap-1;
                if(mid>= arr.length){
                    mid=arr.length-1;
                }
                int right=mid+gap;
                if(right>=arr.length){
                    right=arr.length-1;
                }
                merge(arr,left,mid,right);
            }
            gap*=2;
        }
    }
    public static void countSort(int[] arr){
        int min=arr[0];
        int max=arr[0];
        for (int i = 1; i < arr.length; i++) {
            if(arr[i]<min){
                min=arr[i];
            }
            if(arr[i]>max){
                max=arr[i];
            }
        }
        int[] count=new int[max-min+1];
        for (int i = 0; i < arr.length; i++) {
            int Index=arr[i];
            count[Index-min]++;
        }
        int index=0;
        for (int i = 0; i < count.length; i++) {
            while(count[i]!=0){
                arr[index++]=i+min;
                count[i]--;
            }
        }
    }
}
